home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / tab.zip / TAB.C < prev    next >
C/C++ Source or Header  |  1989-08-15  |  3KB  |  107 lines

  1. /*
  2.  * tab.c - insert a tab into the left margin of a file.  With the -o option
  3.  *         the left margin can be indented a specified number of spaces.
  4.  *         Reads either standard input or the named file and sends output
  5.  *         to standard output.
  6.  *
  7.  * Perry A. D. Wood
  8.  * August 14, 1989
  9.  * Department of Electrical Engineering
  10.  * University of Virginia
  11.  */
  12.  
  13. #include <stdio.h>
  14.  
  15. main(argc, argv)
  16. int argc;
  17. char *argv[];
  18.     {
  19.     int c;               /* data variable */
  20.     int j;               /* general index variable */
  21.     int posit=0;         /* column position, start of line = 0 */
  22.     int opt_char;        /* command line variable */
  23.     int n=0;             /* # of blank spaces to indent */
  24.     FILE *infile;        /* input stream */
  25.     extern int optind, opterr;
  26.     extern char *optarg;
  27.  
  28.     /*
  29.      *  Read the command line options
  30.      */
  31.  
  32.     opterr=1;     /* turns on the error messages printed by getopt */
  33.  
  34.     if ((opt_char = getopt(argc, argv, "o:")) == '?')
  35.         {
  36.     fprintf(stderr, "Usage: tab [-o #_of_spaces] [filename]\n");
  37.         exit();
  38.         }
  39.  
  40.     /* Check for a numeric argument only if the -o option was given */
  41.  
  42.     if ((n = atoi(optarg)) == NULL && opt_char == 'o')
  43.         {
  44.     fprintf(stderr, "%s: non-numeric argument for option -- o\n", argv[0]);
  45.     fprintf(stderr, "Usage: tab [-o #_of_spaces] [filename]\n");
  46.         exit();
  47.         }
  48.  
  49.     /*
  50.      * Determine if the user has specified an input file.  If no file
  51.      * has been specified then use standard input.
  52.      */
  53.  
  54.     if (argv[optind] == NULL)
  55.         infile = stdin;
  56.  
  57.     else
  58.         if ((infile = fopen(argv[optind], "r")) == NULL)
  59.             {
  60.         fprintf(stderr, "%s: unable to open %s\n", argv[0], argv[optind]);
  61.             exit();
  62.         }
  63.  
  64.     /*
  65.      * Indent the input stream, outputting to standard output
  66.      */
  67.  
  68.     switch(opt_char)
  69.         {
  70.         case EOF:         /* no argument so indent one tab */
  71.  
  72.             while((c = getc(infile)) != EOF)
  73.                 {
  74.                 posit++;
  75.  
  76.                 if(c == '\n')  /* reset column position on a new line */
  77.                     posit = 0;
  78.  
  79.                 if(posit == 1)  /* put a tab at the start of the line */
  80.                     printf("\11%c", c);
  81.                 else
  82.                     printf("%c", c);
  83.                 }
  84.             break;
  85.  
  86.         case 'o':       /* indent n spaces */
  87.  
  88.             while((c = getc(infile)) != EOF)
  89.                 {
  90.                 posit++;
  91.  
  92.                 if(c == '\n')  /* reset column position on a new line */
  93.                     posit = 0;
  94.  
  95.                 if(posit == 1)  /* put n spaces at the start of the line */
  96.                     {
  97.                     for(j = 0; j < n; j++)
  98.                         printf(" ");
  99.                     printf("%c", c);
  100.                     }
  101.                 else
  102.                     printf("%c", c);
  103.                 }
  104.             break;
  105.         }
  106.     }
  107.